package com.roncoo.education.course.service.api.pc;

import com.auth0.jwt.interfaces.DecodedJWT;
import com.roncoo.education.course.common.bo.auth.AuthCourseRateBO;
import com.roncoo.education.course.service.api.auth.biz.AuthApiCourseComponent;
import com.roncoo.education.course.service.api.auth.biz.AuthApiCourseRateBiz;
import com.roncoo.education.course.service.dao.impl.mapper.entity.CourseRate;
import com.roncoo.education.util.base.BaseController;
import com.roncoo.education.util.base.Page;
import com.roncoo.education.util.base.Result;
import com.roncoo.education.util.tools.JWTUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;

@Api(description = "课时评论")
@RestController
@RequestMapping(value = "/course/auth/course/rate")
public class AuthApiCourseRateController extends BaseController {

    @Autowired
    private AuthApiCourseRateBiz authApiCourseRateBiz;

    @Autowired
    private AuthApiCourseComponent authApiCourseComponent;

    @ApiOperation("用户新增课时评价")
    @RequestMapping(value = "save",method = RequestMethod.POST)
    public Result<CourseRate> save(@RequestBody @Validated AuthCourseRateBO courseRateBO, @RequestHeader("token")String token){
        Long userNo = null;
        try {
            DecodedJWT verify = JWTUtil.verify(token);
            userNo = JWTUtil.getUserNo(verify);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return Result.error("解析token失败");
        }

        Result check = authApiCourseComponent.check(courseRateBO.getCourseId(), courseRateBO.getChapterId(), courseRateBO.getPeriodId());
        if(check!=null){
            return check;
        }

        CourseRate courseRate = new CourseRate();
        BeanUtils.copyProperties(courseRateBO,courseRate);
        courseRate.setUserNo(userNo);
        int save = authApiCourseRateBiz.save(courseRate);
        if(save>0){
            return Result.success(courseRate) ;
        }
        return Result.error("评价失败，请稍后再试");
    }

    @ApiOperation("用户删除课时评价")
    @RequestMapping(value = "delete",method = RequestMethod.DELETE)
    public Result delete(Long courseRateId){
        int delete = authApiCourseRateBiz.delete(courseRateId);
        if(delete>0){
            return Result.success("删除成功");
        }
        return Result.error("删除失败，请稍后再试");
    }

    @ApiOperation("查看我的评价")
    @RequestMapping(value = "page",method = RequestMethod.POST)
    public Result<Page<CourseRate>> page(@RequestParam(value = "pageCurrent",defaultValue = "1") Integer pageCurrent,
                                         @RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize,
                                         @RequestHeader("token")String token){
        Long userNo = null;
        try {
            DecodedJWT verify = JWTUtil.verify(token);
            userNo = JWTUtil.getUserNo(verify);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return Result.error("解析token失败");
        }
        Page<CourseRate> page = authApiCourseRateBiz.page(pageCurrent, pageSize, userNo);
        return Result.success(page);
    }
}

